package com.zmn.mcc.dubbo.impl.permit;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.gateway.dubbo.client.GUserHolder;
import com.zmn.mcc.business.interfaces.permit.PermitBService;
import com.zmn.mcc.business.interfaces.staff.StaffBService;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.cc.CCDeptDpermitDRO;
import com.zmn.mcc.common.dto.cc.CCPermitDRO;
import com.zmn.mcc.common.dto.cc.UpdateQuery;
import com.zmn.mcc.common.dto.permit.PermitDRO;
import com.zmn.mcc.common.dto.permit.PermitTreeDRO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.permit.PermitListRemoteService;
import com.zmn.mcc.model.entity.dept.McDeptDpermit;
import com.zmn.mcc.model.entity.dept.McDeptDpermitQuery;
import com.zmn.mcc.model.entity.permit.McPermit;
import com.zmn.mcc.model.entity.permit.McPermitQuery;
import com.zmn.mcc.services.interfaces.dept.DeptDpermitService;
import com.zmn.mcc.services.interfaces.permit.PermitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 描述: 权限查询接口
 *
 * @author guoqiao
 * @since 2021/5/8 10:41
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class PermitListRemoteServiceImpl implements PermitListRemoteService {

    @Resource
    private PermitService permitService;

    @Resource
    private PermitBService permitBService;

    @Resource
    private DeptDpermitService deptDpermitService;

    @Resource
    private StaffBService staffBService;

    @Override
    public ResponseDTO<List<CCPermitDRO>> listPermitByUpdateTime(UpdateQuery query) {

        if (Objects.isNull(query.getFrom()) || Objects.isNull(query.getTo())) {
            return ResponseDTO.fail("更新起止时间不能为空");
        }
        try {
            McPermitQuery query2 = new McPermitQuery();
            BeanUtils.copyProperties(query, query2);
            List<McPermit> result = Optional.ofNullable(permitService.listPageByQuery(query2)).orElse(Collections.emptyList());
            List<CCPermitDRO> result2 = BeanMapper.mapList(result, CCPermitDRO.class);
            return ResponseDTO.success(result2);
        } catch (BeansException e) {
            log.error("listBaseAreaByUpdateTime查询失败,原因:{}", e.getMessage(), e);
            return ResponseDTO.fail("查询失败，程序异常");
        }
    }

    @Override
    public ResponseDTO<List<CCDeptDpermitDRO>> listDeptPermitByUpdateTime(Integer pageSize, Integer pageIndex) {

        if (NumberUtil.isNullOrZero(pageSize)) {
            pageSize = 100;
        }
        if (NumberUtil.isNullOrZero(pageIndex)) {
            pageIndex = 1;
        }
        try {
            McDeptDpermitQuery query = new McDeptDpermitQuery();
            query.setPageSize(pageSize);
            query.setPageIndex(pageIndex);
            List<McDeptDpermit> result = Optional.ofNullable(deptDpermitService.listPageByQuery(query)).orElse(Collections.emptyList());
            List<CCDeptDpermitDRO> result2 = BeanMapper.mapList(result, CCDeptDpermitDRO.class);
            return ResponseDTO.success(result2);
        } catch (BeansException e) {
            log.error("listDeptPermitByUpdateTime查询失败,原因:{}", e.getMessage(), e);
            return ResponseDTO.fail("查询失败，程序异常");
        }
    }

    @Override
    public ResponseDTO<List<PermitDRO>> listOrgPermitByStaffId(Integer staffId) {
        return ResponseDTO.success(permitBService.listOrgPermitByStaffId(staffId));
    }

    @Override
    public ResponseDTO<List<PermitTreeDRO>> listProductPermitByStaffId(Integer staffId) {
        return ResponseDTO.success(permitBService.listProductPermitByStaffId(staffId));
    }

    @Override
    public ResponseDTO<List<String>> checkPermitUrls(String app, List<String> urls) {
        LoginStaff staff = GUserHolder.getCurrentLoginStaff();
        if (Objects.isNull(staff)) {
            return ResponseDTO.fail("未登录");
        }

        // 超管直接返回
        if (staffBService.isSuperAdmin(staff.getStaffId())) {
            return ResponseDTO.success(urls);
        }

        return ResponseDTO.success(permitBService.checkPermitUrls(staff.getStaffId(), app, urls));
    }
}
